﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;
using DP.Data.OracleClient;

namespace CRM.Web.ClaimFormManage
{
    public partial class Modify : Common.PageBase
    {
        
        T_DICTIONARY_BLL dBll = new T_DICTIONARY_BLL();
        T_CLAIMFORM_BLL bll = new T_CLAIMFORM_BLL();
        T_CLAIMFORMRESULT_BLL cfrBll = new T_CLAIMFORMRESULT_BLL();
        T_BUSINESSDIVISION_BLL bdBll = new T_BUSINESSDIVISION_BLL();
        T_MARKETDEPARTMENT_BLL marketDepartmentBll = new T_MARKETDEPARTMENT_BLL();
        private OracleDAL<NZ_TCLAIMCARD_Entity> _nzclaimform = new OracleDAL<NZ_TCLAIMCARD_Entity>();
        private OracleDAL<WJJ_TCLAIMCARD_Entity> _wjjclaimform = new OracleDAL<WJJ_TCLAIMCARD_Entity>();
        private OracleDAL<ZZJ_TCLAIMCARD_Entity> _zzjclaimform = new OracleDAL<ZZJ_TCLAIMCARD_Entity>();
        private OracleDAL<QC_TCLAIMCARD_Entity> _qcclaimform = new OracleDAL<QC_TCLAIMCARD_Entity>();
        private OracleDAL<MT_TCLAIMCARD_Entity> _mtclaimform = new OracleDAL<MT_TCLAIMCARD_Entity>();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
            PageHelper.SetPopCalender(this.Page, txtSALEDATE);
            PageHelper.SetPopCalender(this.Page, txtFINISHTIME);
            PageHelper.SetPopCalender(this.Page, txtREPDATE);
            PageHelper.SetPopCalender(this.Page, txtFEEDBACKDATE);
            //PageHelper.SetPopCalender(this.Page, txtALLOCATEDTIME);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlIsServiceCardBinding();
                ddlSTATUSBinding();
                ddlPRODUCTSATISFACTIONBinding();
                ddlSERVICESATISFACTIONBinding();
                ddlSPTYPEBinding();
                ddlISALLARRIVEBinding();
                ddlMarketDepartmentCodeBinding();
                ddlSERVICECARDTYPENAMEBinding();
                ddlISFEEDBACKBinding();
                pageDataInit();
            }
        }

       

        #region 页面初始化数据绑定

        private void ddlIsServiceCardBinding()
        {
            EnumHelper.FillListControl(ddlIsServiceCard, typeof(IsServiceCard));
            ddlIsServiceCard.Items.Insert(0, new ListItem("", ""));
        }
        /// <summary>
        /// DDLs the market department code binding.
        /// </summary>
        private void ddlMarketDepartmentCodeBinding()
        {
            ddlMarketDepartmentCode.DataSource = marketDepartmentBll.GetCache_T_MARKETDEPARTMENT_List(this.Page).FindAll(p => p.BUSINESSDIVISIONCODE == _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE);
            ddlMarketDepartmentCode.DataTextField = "MARKETDEPARTMENTNAME";
            ddlMarketDepartmentCode.DataValueField = "MARKETDEPARTMENTCODE";
            ddlMarketDepartmentCode.DataBind();
            ddlMarketDepartmentCode.Items.Insert(0, new ListItem("", ""));
        }

        private void ddlISALLARRIVEBinding()
        {
            ddlISALLARRIVE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("IsAllArrive"));
            ddlISALLARRIVE.DataTextField = "DictValue";
            ddlISALLARRIVE.DataValueField = "DictKey";
            ddlISALLARRIVE.DataBind();
            ddlISALLARRIVE.Items.Insert(0, new ListItem("", ""));
        }
        /// <summary>
        /// DDLs the SPTYPE binding.
        /// </summary>
        private void ddlSPTYPEBinding()
        {
            ddlSPTYPE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("ClaimFormSPType"));
            ddlSPTYPE.DataTextField = "DictValue";
            ddlSPTYPE.DataValueField = "DictKey";
            ddlSPTYPE.DataBind();
            ddlSPTYPE.Items.Insert(0, new ListItem("", ""));
        }


        private void ddlSTATUSBinding()
        {
            ddlSTATUS.Items.Clear();
            EnumHelper.FillListControl(ddlSTATUS, typeof(ClaimStatus));
            ddlSTATUS.Items.Insert(0, new ListItem("", "")); 
        }
        
        private void ddlPRODUCTSATISFACTIONBinding()
        {
            ddlPRODUCTSATISFACTION.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("ClaimFormProductSatisfaction"));
            ddlPRODUCTSATISFACTION.DataTextField = "DictValue";
            ddlPRODUCTSATISFACTION.DataValueField = "DictKey";
            ddlPRODUCTSATISFACTION.DataBind();
            ddlPRODUCTSATISFACTION.Items.Insert(0, new ListItem("", ""));

        }

        private void ddlSERVICESATISFACTIONBinding()
        {
            ddlSERVICESATISFACTION.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("ClaimFormServiceSatisfaction"));
            ddlSERVICESATISFACTION.DataTextField = "DictValue";
            ddlSERVICESATISFACTION.DataValueField = "DictKey";
            ddlSERVICESATISFACTION.DataBind();
            ddlSERVICESATISFACTION.Items.Insert(0, new ListItem("", ""));
        }



        private void ddlISFEEDBACKBinding()
        {
            ddlISFEEDBACK.Items.Clear();
            EnumHelper.FillListControl(ddlISFEEDBACK, typeof(IsFeedBack));
            ddlISFEEDBACK.Items.Insert(0, new ListItem("", ""));
            //ddlISFEEDBACK.SelectedValue = "0";

        }



        /// <summary>
        /// DDLs the SPTYPE binding.
        /// </summary>
        private void ddlSERVICECARDTYPENAMEBinding()
        {
            ddlSERVICECARDTYPENAME.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("ClaimFormServiceCardType"));
            ddlSERVICECARDTYPENAME.DataTextField = "DictValue";
            ddlSERVICECARDTYPENAME.DataValueField = "DictKey";
            ddlSERVICECARDTYPENAME.DataBind();
            ddlSERVICECARDTYPENAME.Items.Insert(0, new ListItem("", ""));
        }
       







        #endregion

        private void pageDataInit()
        {
            string _claimformid = String.IsNullOrEmpty(Request.QueryString["CLAIMFORMID"]) ? "" : Request.QueryString["CLAIMFORMID"].Trim();

            if (!String.IsNullOrEmpty(_claimformid))
            {
                T_CLAIMFORM_Entity entity = bll.GetEntity("CLAIMFORMID", _claimformid);

                PageHelper.SetControlValue<T_CLAIMFORM_Entity>(this.Page, entity);
                PageHelper.SetControlEnabled(this.phClaimForm, false);
                txtFEEDBACKDATE.Text = entity.FEEDBACKDATE.HasValue ? entity.FEEDBACKDATE.Value.ToString("yyyy-MM-dd HH:mm:ss") : "";
                if(entity.ISFEEDBACK.ToString() == ((int)IsFeedBack.已回访).ToString())
                {
                    PageHelper.SetControlEnabled(this.phFeedBack, false);
                    btnNotPass.Visible = false;
                    btnPass.Visible = false;
                    btnReject.Visible = false;
                    btnRecord.Visible = false;
                }
                //if(String.IsNullOrEmpty(entity.ASSIGNEDPERSONS.ToString()))
                //{
                //    PageHelper.SetControlEnabled(this.phFeedBack, false);
                //    btnNotPass.Visible = false;
                //    btnPass.Visible = false;
                //    btnReject.Visible = false;
                //}
                frameClaimItem.Attributes["src"] = String.Format("ClaimItemList.aspx?PARENTID={0}", entity.OBJID.Trim());
            }

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string _claimformid = String.IsNullOrEmpty(Request.QueryString["CLAIMFORMID"]) ? "" : Request.QueryString["CLAIMFORMID"].Trim();

            T_CLAIMFORMRESULT_Entity cfrEntity = new T_CLAIMFORMRESULT_Entity();
            T_CLAIMFORM_Entity entity = bll.GetEntity("CLAIMFORMID", _claimformid);

            if (entity != null)
            {
                Button btn = sender as Button;
                if(btn == null) return;

                if (btn.ID == "btnRecord")
                {
                    PageHelper.GetControlValue<T_CLAIMFORM_Entity>(this.Page, ref entity);
                    entity.ISFEEDBACK = (int)IsFeedBack.回访不成功;
                    entity.FEEDBACKDATE = DateTime.Now;
                    entity.ASSIGNEDPERSONNAME = _userLoginInfo.UserEntity.USERNAME;
                }
                else
                {
                    int status = StringHelper.ToInt(entity.STATUS.ToString(), 0);
                    switch (btn.CommandName)
                    {
                        case "Pass":
                            {
                                status = (int) ClaimStatus.回访审核通过;
                            }
                            break;
                        case "NotPass":
                            {
                                status = (int) ClaimStatus.回访审核不通过;
                            }
                            break;
                        case "Reject":
                            {
                                status = (int) ClaimStatus.新建;
                            }
                            break;
                    }
                    PageHelper.GetControlValue<T_CLAIMFORM_Entity>(this.Page, ref entity);
                    entity.ISFEEDBACK = (int) IsFeedBack.已回访;
                    entity.FEEDBACKDATE = DateTime.Now;
                    entity.STATUS = status;
                    entity.ASSIGNEDPERSONNAME = _userLoginInfo.UserEntity.USERNAME;

                    //PageHelper.GetControlValue<T_CLAIMFORMRESULT_Entity>(this.phFeedBack, ref cfrEntity);
                    //cfrEntity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                    //cfrEntity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                    //cfrEntity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                    //cfrEntity.CLAIMFROMOBJID = entity.OBJID;
                    //cfrEntity.CLAIMFROMID = entity.CLAIMFORMID;
                    //cfrEntity.CREATEPEOPLE = _userLoginInfo.UserEntity.CREATEPEOPLE;
                    //cfrEntity.CREATEPEOPLEID = _userLoginInfo.UserEntity.CREATEPEOPLEID;
                    //cfrEntity.CREATETIME = DateTime.Now;
                    //cfrEntity.FEEDBACKDATE = DateTime.Now;
                    //cfrEntity.FTIMESTAMP = DateTime.Now;
                    //cfrEntity.STATUS = btn.Text;
                    //cfrEntity.ISSERVICECARD = StringHelper.ToInt(ddlIsServiceCard.SelectedValue, 0);

                    //cfrBll.Insert(cfrEntity);

                    #region 回写VEI中间表索赔单
                    if (entity.BUSINESSDIVISIONCODE == "8101")//农装
                    {
                        NZ_TCLAIMCARD_Entity nzclaimform = new NZ_TCLAIMCARD_Entity();
                        nzclaimform = _nzclaimform.GetEntity("OBJID", entity.OBJID);
                        if (nzclaimform != null)
                        {
                            nzclaimform.ISFEEDBACK = (int)IsFeedBack.已回访; //是否回访
                            nzclaimform.STATUS = status;//单据状态
                            nzclaimform.FEEDBACKIDEA = entity.FEEDBACKIDEA; //回访意见
                            try
                            {
                                nzclaimform.PRODUCTSATISFACTION = entity.PRODUCTSATISFACTION == "" ? 0 : int.Parse(entity.PRODUCTSATISFACTION);
                                nzclaimform.SERVICESATISFACTION = entity.SERVICESATISFACTION == "" ? 0 : int.Parse(entity.SERVICESATISFACTION);
                            }
                            catch { }
                            nzclaimform.NONESATISFACTION = entity.SATISFIEDREASONS;
                            nzclaimform.CC_UPDATE = 1;
                            _nzclaimform.Update(nzclaimform);
                        }
                    }
                    else if (entity.BUSINESSDIVISIONCODE == "8106")//挖掘机
                    {
                        WJJ_TCLAIMCARD_Entity wjjclaimform = new WJJ_TCLAIMCARD_Entity();
                        wjjclaimform = _wjjclaimform.GetEntity("OBJID", entity.OBJID);
                        if (wjjclaimform != null)
                        {
                            wjjclaimform.ISFEEDBACK = (int)IsFeedBack.已回访;
                            wjjclaimform.STATUS = status;
                            wjjclaimform.FEEDBACKIDEA = entity.FEEDBACKIDEA;
                            wjjclaimform.CC_UPDATE = 1;
                            _wjjclaimform.Update(wjjclaimform);
                        }
                    }
                    else if (entity.BUSINESSDIVISIONCODE == "8102")//装载机
                    {
                        ZZJ_TCLAIMCARD_Entity zzjclaimform = new ZZJ_TCLAIMCARD_Entity();
                        zzjclaimform = _zzjclaimform.GetEntity("OBJID", entity.OBJID);
                        if (zzjclaimform != null)
                        {
                            zzjclaimform.ISFEEDBACK = (int)IsFeedBack.已回访;
                            zzjclaimform.STATUS = status;
                            zzjclaimform.FEEDBACKIDEA = entity.FEEDBACKIDEA;
                            zzjclaimform.CC_UPDATE = 1;
                            _zzjclaimform.Update(zzjclaimform);
                        }
                    }
                    else if (entity.BUSINESSDIVISIONCODE == "8103")//五星汽车
                    {
                        QC_TCLAIMCARD_Entity qcclaimform = new QC_TCLAIMCARD_Entity();
                        qcclaimform = _qcclaimform.GetEntity("OBJID", entity.OBJID);
                        if (qcclaimform != null)
                        {
                            qcclaimform.ISFEEDBACK = (int)IsFeedBack.已回访;
                            qcclaimform.STATUS = status;
                            qcclaimform.ISREVIEW = (int)IsFeedBack.已回访;
                            qcclaimform.REVIEWREMARK = entity.FEEDBACKIDEA;
                            qcclaimform.REVIEWTIME = entity.FEEDBACKDATE;
                            qcclaimform.CC_UPDATE = 1;
                            _qcclaimform.Update(qcclaimform);
                        }
                    }
                    else if (entity.BUSINESSDIVISIONCODE == "8104")//五星摩托
                    {
                        MT_TCLAIMCARD_Entity mtclaimform = new MT_TCLAIMCARD_Entity();
                        mtclaimform = _mtclaimform.GetEntity("OBJID", entity.OBJID);
                        if (mtclaimform != null)
                        {
                            mtclaimform.ISFEEDBACK = (int)IsFeedBack.已回访;
                            mtclaimform.STATUS = status;
                            mtclaimform.ISREVIEW = (int)IsFeedBack.已回访;
                            mtclaimform.REVIEWREMARK = entity.FEEDBACKIDEA;
                            mtclaimform.CC_UPDATE = 1;
                            _mtclaimform.Update(mtclaimform);
                        }
                    }
                    #endregion
                }
                try
                {
                    long rel = bll.Update(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, String.Format("{0} 成功！", btn.Text), UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this), "list.aspx"));
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, String.Format("{0} 失败！", btn.Text));
                    }
                }
                catch (Exception ex)
                {
                    
                        PageHelper.ShowMessage(this.Page, String.Format("{0} 失败！{1}", btn.Text, ex.Message));
                }
            }
        }
    }
}
